package com.jinwei.qunguanjia.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dao.GroupCommentDAO;
import com.jinwei.qunguanjia.user.dao.GroupCommentReplyDAO;
import com.jinwei.qunguanjia.user.dao.GroupDAO;
import com.jinwei.qunguanjia.user.dao.GroupLikeDAO;
import com.jinwei.qunguanjia.user.dto.GroupLikeDTO;
import com.jinwei.qunguanjia.user.po.GroupLikePO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service("groupLikeService")
public class GroupLikeServiceImpl implements GroupLikeService {

    @Resource
    private GroupDAO groupDAO;
    @Resource
    private GroupLikeDAO groupLikeDAO;
    @Resource
    private GroupCommentDAO groupCommentDAO;
    @Resource
    private GroupCommentReplyDAO groupCommentReplyDAO;

    @Override
    public String save(GroupLikeDTO groupLikeDTO) {
        GroupLikePO groupLikePO = new GroupLikePO();
        BeanUtils.copyProperties(groupLikeDTO, groupLikePO);
        String groupLikeId = IdUtils.getWorkerId();
        groupLikePO.setGroupLikeId(groupLikeId);
        groupLikePO.setCreateTime(DateUtils.getDateTime());
        groupLikeDAO.insert(groupLikePO);
        if (null != groupLikeDTO.getGroupId() && "-1".equals(groupLikeDTO.getGroupCommentId()) && "-1".equals(groupLikeDTO.getGroupCommentReplyId())) {
            groupDAO.addLikeCount(groupLikeDTO.getGroupId());
        } else if (null != groupLikeDTO.getGroupId() && null != groupLikeDTO.getGroupCommentId() && "-1".equals(groupLikeDTO.getGroupCommentReplyId())) {
            groupCommentDAO.addLikeCount(groupLikeDTO.getGroupCommentId());
        } else if (null != groupLikeDTO.getGroupId() && null != groupLikeDTO.getGroupCommentId() && null != groupLikeDTO.getGroupCommentReplyId()) {
            groupCommentReplyDAO.addLikeCount(groupLikeDTO.getGroupCommentReplyId());
        }
        return groupLikeId;
    }

    @Transactional
    @Override
    public void remove(String id) {
        GroupLikePO groupLikePO = groupLikeDAO.selectById(id);
        if (null != groupLikePO.getGroupId() && "-1".equals(groupLikePO.getGroupCommentId()) && "-1".equals(groupLikePO.getGroupCommentReplyId())) {
            groupDAO.subtractLikeCount(groupLikePO.getGroupId());
        } else if (null != groupLikePO.getGroupId() && null != groupLikePO.getGroupCommentId() && "-1".equals(groupLikePO.getGroupCommentReplyId())) {
            groupCommentDAO.subtractLikeCount(groupLikePO.getGroupCommentId());
        } else if (null != groupLikePO.getGroupId() && null != groupLikePO.getGroupCommentId() && groupLikePO.getGroupCommentReplyId() != null) {
            groupCommentReplyDAO.subtractLikeCount(groupLikePO.getGroupCommentReplyId());
        }
        groupLikeDAO.deleteById(id);
    }

    @Override
    public void update(GroupLikeDTO groupLikeDTO) {
        GroupLikePO groupLikePO = new GroupLikePO();
        BeanUtils.copyProperties(groupLikeDTO, groupLikePO);
        groupLikeDAO.updateById(groupLikePO);
    }

    @Override
    public GroupLikeDTO get(String id) {
        GroupLikePO groupLikePO = groupLikeDAO.selectById(id);
        if (null == groupLikePO) {
            return null;
        }
        GroupLikeDTO groupLikeDTO = new GroupLikeDTO();
        BeanUtils.copyProperties(groupLikePO, groupLikeDTO);
        return groupLikeDTO;
    }

    @Override
    public GroupLikeDTO get(GroupLikeDTO groupLikeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupLikeDTO);
        // 拼接查询条件
        QueryWrapper<GroupLikePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        GroupLikePO groupLikePO = groupLikeDAO.selectOne(queryWrapper);
        if (null == groupLikePO) {
            return null;
        }
        BeanUtils.copyProperties(groupLikePO, groupLikeDTO);
        return groupLikeDTO;
    }

    @Override
    public Long count(GroupLikeDTO groupLikeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupLikeDTO);
        // 拼接查询条件
        QueryWrapper<GroupLikePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return groupLikeDAO.selectCount(queryWrapper);
    }

    @Override
    public List<GroupLikeDTO> list(GroupLikeDTO groupLikeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupLikeDTO);
        // 拼接查询条件
        QueryWrapper<GroupLikePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return JsonUtils.copyList(groupLikeDAO.selectList(queryWrapper), GroupLikeDTO.class);
    }

    @Override
    public PageDataInfo<GroupLikeDTO> listPage(GroupLikeDTO groupLikeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupLikeDTO);
        // 拼接查询条件
        QueryWrapper<GroupLikePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<GroupLikePO> list = groupLikeDAO.selectList(queryWrapper);
        PageInfo<GroupLikePO> pageInfo = new PageInfo<>(list);
        PageDataInfo<GroupLikeDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(JsonUtils.copyList(list, GroupLikeDTO.class));
        pageDataInfo.setTotal(pageInfo.getTotal());
        pageDataInfo.setTotalPage(PageCountUtils.getPageCount(pageInfo.getTotal(), pageInfo.getPageSize()));
        return pageDataInfo;
    }

    @Override
    public String checkUnique(GroupLikeDTO groupLikeDTO) throws IllegalAccessException {
        String id = StringUtils.isNull(groupLikeDTO.getGroupLikeId()) ? "-1" : groupLikeDTO.getGroupLikeId();
        GroupLikeDTO info = get(groupLikeDTO);
        if (StringUtils.isNotNull(info) && !info.getGroupId().equals(id)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    
}
